﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Dependencies;
using System.Web.Mvc;
using IMvcDependencyResolver = System.Web.Mvc.IDependencyResolver;
using IWebApiDependencyResolver = System.Web.Http.Dependencies.IDependencyResolver;

namespace UT_WebAPI
{
    class MefUtConfig
    {
        private static CompositionContainer _container;

        public static MefWebDependencySolver RegisterSolver()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(Directory.GetCurrentDirectory()));
            var solver = new MefWebDependencySolver(catalog);
            DependencyResolver.SetResolver(solver);
            GlobalConfiguration.Configuration.DependencyResolver = solver;
            _container = new CompositionContainer(catalog);
            return solver;
        }

        internal class MefWebDependencySolver : IMvcDependencyResolver, IWebApiDependencyResolver
        {
            #region 枚举

            //private const string HttpContextKey = "MefContainerKey";
            private readonly ComposablePartCatalog _catalog;

            #endregion 枚举

            #region 属性

            //private CompositionContainer Container
            //{
            //    get
            //    {
            //        if (!HttpContext.Current.Items.Contains(HttpContextKey))
            //        {
            //            HttpContext.Current.Items.Add(HttpContextKey, new CompositionContainer(_catalog));
            //        }
            //        var container = (CompositionContainer)HttpContext.Current.Items[HttpContextKey];
            //        HttpContext.Current.Application["Container"] = container;
            //        return container;
            //    }
            //}

            #endregion 属性

            #region 构造

            public MefWebDependencySolver(ComposablePartCatalog catalog)
            {
                _catalog = catalog;

            }

            #endregion 构造

            #region 方法

            public object GetService(Type serviceType)
            {
                var contractName = AttributedModelServices.GetContractName(serviceType);
                var service = _container.GetExportedValueOrDefault<object>(contractName);
                return service;
            }

            public IEnumerable<object> GetServices(Type serviceType)
            {
                return _container.GetExportedValues<object>(serviceType.FullName);
            }

            public IDependencyScope BeginScope()
            {
                return this;
            }

            #endregion 方法

            public void Dispose()
            {
            }
        }
    }
}